﻿
using Lib.Data.Interface;
using System;
using System.Collections;
using System.Data;
using System.Data.Common;

namespace Lib.Data.Entitys; 
/// <summary>
/// 
/// </summary>
public sealed class DataReader : IDataReader, IDataRecord, INullableReader {
    /// <summary>
    /// 
    /// </summary>
    private readonly IDataReader Rs;
    /// <summary>
    /// 
    /// </summary>
    private readonly Hashtable Fields;
    /// <summary>
    /// 构造函数
    /// </summary>
    private DataReader() {}
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="dataReader"></param>
    public DataReader(DbDataReader DataReader) {
        if(DataReader != null) {
            this.Rs = DataReader;
            this.HasRows = DataReader.HasRows;
            this.Fields = new Hashtable(StringComparer.OrdinalIgnoreCase);
            for (int i = 0; i < this.Rs.FieldCount; i++) {
                Fields.Add(this.Rs.GetName(i), i);
            }
        }
    }
    /// <summary>
    /// 关闭对象
    /// </summary>
    public void Close() {
        this.Rs.Close();
    }
    /// <summary>
    /// 释放资源
    /// </summary>
    public void Dispose() {
        this.Rs?.Dispose();
    }
    /// <summary>
    /// 获取指定列的布尔值形式的值
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public bool GetBoolean(int i) {
        return this.Rs.GetBoolean(i);
    }
    /// <summary>
    /// 获取指定列的布尔值形式的值
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public bool GetBoolean(string Name) {
        if (!this.IsDBNull(Name)) {
            return this.GetBoolean(this.GetOrdinal(Name));
        }
        return false;
    }
    /// <summary>
    /// 获取指定字段的日期与时间数据值
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public DateTime GetDateTime(int i) {
        return this.Rs.GetDateTime(i);
    }
    /// <summary>
    /// 获取指定字段的日期与时间数据值
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public DateTime GetDateTime(string Name) {
        if (!this.IsDBNull(Name)) {
            return this.Rs.GetDateTime(this.GetOrdinal(Name));
        }
        return DateTime.Now;
    }
    /// <summary>
    /// 获取指定字段的日期与时间(可为空)数据值
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public DateTime? GetTime(int i) {
        if (!this.IsDBNull(i)) {
            return new Nullable<DateTime>(this.Rs.GetDateTime(i));
        }
        return null;
    }
    /// <summary>
    /// 获取指定字段的日期与时间(可为空)数据值
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public DateTime? GetTime(string Name) {
        if (!this.IsDBNull(Name)) {
            return new Nullable<DateTime>(this.Rs.GetDateTime(this.GetOrdinal(Name)));
        }
        return null;
    }
    /// <summary>
    /// 获取指定字段的高精度浮点数(Decimal)[十进制数]
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public decimal GetDecimal(int i) {
        return this.Rs.GetDecimal(i);
    }
    /// <summary>
    /// 获取指定字段的高精度浮点数(Decimal)[十进制数]
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public decimal GetDecimal(string Name) {
        if (!this.IsDBNull(Name)) {
            return this.Rs.GetDecimal(this.GetOrdinal(Name));
        }
        return 0;
    }
    /// <summary>
    /// 获取指定字段的双精度浮点数(Double)
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public double GetDouble(int i) {
        return this.Rs.GetDouble(i);
    }
    /// <summary>
    /// 获取指定字段的双精度浮点数(Double)
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public double GetDouble(string Name) {
        if (!this.IsDBNull(Name)) {
            return this.Rs.GetDouble(this.GetOrdinal(Name));
        }
        return 0;
    }
    /// <summary>
    /// 获取指定字段的单精度浮点数(float)
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public float GetFloat(int i) {
        return this.Rs.GetFloat(i);
    }
    /// <summary>
    /// 获取指定字段的单精度浮点数(float)
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public float GetFloat(string Name) {
        if (!this.IsDBNull(Name)) {
            return this.Rs.GetFloat(this.GetOrdinal(Name));
        }
        return 0;
    }
    /// <summary>
    /// 获取指定字段的单精度浮点数(float)
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public float GetSingle(int i) {
        return this.Rs.GetFloat(i);
    }
    /// <summary>
    /// 获取指定字段的单精度浮点数(float)
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public float GetSingle(string Name) {
        if (!this.IsDBNull(Name)) {
            return this.Rs.GetFloat(this.GetOrdinal(Name));
        }
        return 0;
    }
    /// <summary>
    /// 获取指定字段的16位有符号整数值(short)
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public short GetInt16(int i) {
        return this.Rs.GetInt16(i);
    }
    /// <summary>
    /// 获取指定字段的16位有符号整数值(short)
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public short GetInt16(string Name) {
        if (!this.IsDBNull(Name)) {
            return this.Rs.GetInt16(this.GetOrdinal(Name));
        }
        return 0;
    }
    /// <summary>
    /// 获取指定字段的32位有符号整数值(int)
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public int GetInt32(int i) {
        return this.Rs.GetInt32(i);
    }
    /// <summary>
    /// 获取指定字段的32位有符号整数值(int)
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public int GetInt32(string Name) {
        if (!this.IsDBNull(Name)) {
            return this.Rs.GetInt32(this.GetOrdinal(Name));
        }
        return 0;
    }
    /// <summary>
    /// 获取指定字段的64位有符号整数值(long)
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public long GetInt64(int i) {
        return this.Rs.GetInt64(i);
    }
    /// <summary>
    /// 获取指定字段的64位有符号整数值(long)
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public long GetInt64(string Name) {
        if (!this.IsDBNull(Name)) {
            return this.Rs.GetInt64(this.GetOrdinal(Name));
        }
        return 0L;
    }
    /// <summary>
    /// 获取指定字段的字符串值
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public string GetString(int i) {
        return this.Rs.GetString(i);
    }
    /// <summary>
    /// 获取指定字段的字符串值
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public string GetString(string Name) {
        if (!this.IsDBNull(Name)) {
            return this.Rs.GetString(this.GetOrdinal(Name));
        }
        return string.Empty;
    }
    /// <summary>
    /// 返回指定字段的GUID值
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public Guid GetGuid(int i) {
        return this.Rs.GetGuid(i);
    }
    /// <summary>
    /// 返回指定字段的GUID值
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public Guid GetGuid(string Name) {
        return this.Rs.GetGuid(this.GetOrdinal(Name));
    }
    /// <summary>
    /// 返回指定字段的值(object)
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public object GetValue(int i) {
        return this.IsDBNull(i) ? null : this.Rs.GetValue(i);
    }
    /// <summary>
    /// 返回指定字段的值(object)
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public object GetValue(string Name) {
        return this.GetValue(this.GetOrdinal(Name));
    }
    /// <summary>
    /// 返回指定字段的值(object)
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public object this[int i] {
        get {
            return this.Rs[i];
        }
    }
    /// <summary>
    /// 返回指定字段的值(object)
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public object this[string Name] {
        get {
            return this.Rs[Name];
        }
    }
    /// <summary>
    /// 获取指定列的8位无符号整数值
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public byte GetByte(int i) {
        return this.Rs.GetByte(i);
    }
    /// <summary>
    /// 获取指定列的8位无符号整数值
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public byte GetByte(string Name) {
        if (!this.IsDBNull(Name)) {
            return this.GetByte(this.GetOrdinal(Name));
        }
        return 0;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="i"></param>
    /// <param name="fieldOffset"></param>
    /// <param name="buffer"></param>
    /// <param name="bufferoffset"></param>
    /// <param name="length"></param>
    /// <returns></returns>
    public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) {
        return this.Rs.GetBytes(i, fieldOffset, buffer, bufferoffset, length);
    }
    /// <summary>
    /// 获取指定列的字符值
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public char GetChar(int i) {
        return this.Rs.GetChar(i);
    }
    /// <summary>
    /// 获取指定列的字符值
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public char GetChar(string Name) {
        return this.GetChar(this.GetOrdinal(Name));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="i"></param>
    /// <param name="fieldoffset"></param>
    /// <param name="buffer"></param>
    /// <param name="bufferoffset"></param>
    /// <param name="length"></param>
    /// <returns></returns>
    public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) {
        return this.Rs.GetChars(i, fieldoffset, buffer, bufferoffset, length);
    }
    /// <summary>
    /// 返回指定的序列号的IDataReader
    /// </summary>
    /// <param name="i">字段索引</param>
    /// <returns></returns>
    public IDataReader GetData(int i) {
        return this.Rs.GetData(i);
    }
    /// <summary>
    /// 获取指定字段的数据类型信息（数据类型名称）
    /// </summary>
    /// <param name="i">字段索引</param>
    /// <returns></returns>
    public string GetDataTypeName(int i) {
        return this.Rs.GetDataTypeName(i);
    }
    /// <summary>
    /// 获取指定字段的数据类型信息（数据类型名称）
    /// </summary>
    /// <param name="Name">字段名</param>
    /// <returns></returns>
    public string GetDataTypeName(string Name) {
        return this.Rs.GetDataTypeName(this.GetOrdinal(Name));
    }
    /// <summary>
    /// 获取字段的数据类型（System.Type）
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public Type GetFieldType(int i) {
        return this.Rs.GetFieldType(i);
    }
    /// <summary>
    /// 获取字段的数据类型（System.Type）
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public Type GetFieldType(string Name) {
        return this.Rs.GetFieldType(this.GetOrdinal(Name));
    }
    /// <summary>
    /// 获取要查找的字段名称
    /// </summary>
    /// <param name="i">要查找的字段索引</param>
    /// <returns></returns>
    public string GetName(int i) {
        return this.Rs.GetName(i);
    }
    /// <summary>
    /// 返回指定字段名的索引
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public int GetOrdinal(string Name) {
        return this.Rs.GetOrdinal(Name);
    }
    /// <summary>
    /// 判断 DataReader 是否包含指定字段
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public bool Contains(string Name) {
        return Fields.ContainsKey(Name);
    }
    /// <summary>
    /// 返回一个DataTable,它描述 IDataReader 的列元数据
    /// </summary>
    /// <returns></returns>
    public DataTable GetSchemaTable() {
        return this.Rs.GetSchemaTable();
    }
    /// <summary>
    /// 使用当前记录的列值来填充对象数组
    /// </summary>
    /// <param name="values"></param>
    /// <returns></returns>
    public int GetValues(object[] values) {
        return this.Rs.GetValues(values);
    }
    /// <summary>
    /// 返回将指定字段是否为空
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public bool IsDBNull(int i) {
        return this.Rs.IsDBNull(i);
    }
    /// <summary>
    /// 返回将指定字段是否为空
    /// </summary>
    /// <param name="Name"></param>
    /// <returns></returns>
    public bool IsDBNull(string Name) {
        if (this.Fields.ContainsKey(Name)) {
            return this.Rs.IsDBNull(this.GetOrdinal(Name));
        }
        return true;
    }
    /// <summary>
    /// 当读取批处理SQL语句的结果时，使数据读取器前进到下一个结果
    /// </summary>
    /// <returns></returns>
    public bool NextResult() {
        return this.Rs.NextResult();
    }
    /// <summary>
    /// 使 IDataReader 前进到下一条记录
    /// </summary>
    /// <returns></returns>
    public bool Read() {
        return this.Rs.Read();
    }
    /// <summary>
    /// 获取一个值，该值指示当前行的嵌套深度
    /// </summary>
    public int Depth {
        get {
            return this.Rs.Depth;
        }
    }
    /// <summary>
    /// 是否包含一个或多个行
    /// </summary>
    public bool HasRows { get; private set; }
    /// <summary>
    /// 获取当前行的列数
    /// </summary>
    public int FieldCount {
        get {
            return this.Rs.FieldCount;
        }
    }
    /// <summary>
    /// 获取一个值，该值指示数据读取器是否已关闭
    /// </summary>
    public bool IsClosed {
        get {
            return this.Rs.IsClosed;
        }
    }
    /// <summary>
    /// 通过执行SQL语句获取更改、插入、删除的行数
    /// </summary>
    public int RecordsAffected {
        get {
            return this.Rs.RecordsAffected;
        }
    }
}
